<?php
define('DB_DRIVER', 'mysql');
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'cakephp');

final class MySQL {
	private $connection;
	
	public function __construct($hostname, $username, $password, $database) {
		if (!$this->connection = mysql_connect($hostname, $username, $password)) {
      		exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
    	}

    	if (!mysql_select_db($database, $this->connection)) {
      		exit('Error: Could not connect to database ' . $database);
    	}
		
		mysql_query("SET NAMES 'utf8'", $this->connection);
		mysql_query("SET CHARACTER SET utf8", $this->connection);
		mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection);
		mysql_query("SET SQL_MODE = ''", $this->connection);
  	}
		
  	public function query($sql) {
		$resource = mysql_query($sql, $this->connection);

		if ($resource) {
			if (is_resource($resource)) {
				$i = 0;
    	
				$data = array();
		
				while ($result = mysql_fetch_assoc($resource)) {
					$data[$i] = $result;
    	
					$i++;
				}
				
				mysql_free_result($resource);
				
				$query = new stdClass();
				$query->row = isset($data[0]) ? $data[0] : array();
				$query->rows = $data;
				$query->num_rows = $i;
				
				unset($data);

				return $query;	
    		} else {
				return TRUE;
			}
		} else {
			exit('Error: ' . mysql_error($this->connection) . '<br />Error No: ' . mysql_errno($this->connection) . '<br />' . $sql);
    	}
  	}
	
	public function escape($value) {
		return mysql_real_escape_string($value, $this->connection);
	}
	
  	public function countAffected() {
    	return mysql_affected_rows($this->connection);
  	}

  	public function getLastId() {
    	return mysql_insert_id($this->connection);
  	}	
	
	public function __destruct() {
		mysql_close($this->connection);
	}
}


final class DB {
	private $driver;
	static $counter = 0;
	static $queries = array();
	
	public function __construct($driver, $hostname, $username, $password, $database) {

		switch($driver) {
			default : 
				$this->driver = new MySQL($hostname, $username, $password, $database);
			break;
		}
	}
		
  	public function query($sql) {
  		self::$counter++;
  		self::$queries[] = $sql;
		return $this->driver->query($sql);
  	}
	
	public function escape($value) {
		return $this->driver->escape($value);
	}
	
  	public function countAffected() {
		return $this->driver->countAffected();
  	}

  	public function getLastId() {
		return $this->driver->getLastId();
  	}
	
	public function one($table_name, $conditions = array(), $columns = array()) {
		$column = "";
		$condition = "";
		
		if(is_array($conditions) && count($conditions) > 0) :
		$condition = " WHERE 1=1 ";
		foreach($conditions as $name => $value) {
			$condition .= " AND `$name` = '$value' ";
		}		
		endif;
		
		if(is_array($columns) && count($columns) > 0) {
			
			foreach($columns as $name) {
				$column .= ' `$name`, ';
			}
				$column = substr($column,0,strlen($column)-2).' ';

				$sql = "SELECT $column FROM `$table_name`".$condition." LIMIT 1";
		} else if(!empty($columns)) {
				$sql = "SELECT $columns FROM `$table_name`".$condition." LIMIT 1";
		} else {
				$sql = "SELECT * FROM `$table_name`".$condition." LIMIT 1";
		}
		
		
		
		$query = $this->driver->query($sql);
		
		if(count($query->row) < 1) {
			$data = array();
			$query = $this->driver->query("show columns from `stocks`");
			foreach($query->rows as $key => $each_column) {
				$data[$each_column['Field']] = $key;	
			}
			
			return $data;
		}
		
		return $query->row;
  	}
	
	public function all($table_name, $conditions = array(), $limits = array(), $columns = array()) {
		$limit = "";
		$column = "";
		$condition = "";

		if(is_array($conditions) && count($conditions) > 0) :
		$condition = " WHERE 1=1 ";
		foreach($conditions as $name => $value) {
			$condition .= " AND `$name` = '$value' ";
		}
		endif;
		
		if(is_array($limits) && count($limits) > 0) {
			$page = (int) $limits[0] - 1;
			$views = (int) $limits[1] - 1;		
			$limit = " LIMIT $page , $views";
		} else if(!empty($limits)) {
				$limit = " LIMIT $limits ";
		}
		
		if(is_array($columns) && count($columns) > 0) {
			
			foreach($columns as $name) {
				$column .= ' `$name`, ';
			}
				$column = substr($column,0,strlen($column)-2).' ';

				$sql = "SELECT $column FROM `$table_name`".$condition.$limit;
		} else if(!empty($columns)) {
				$sql = "SELECT $columns FROM `$table_name`".$condition.$limit;
		} else {
				$sql = "SELECT * FROM `$table_name`".$condition.$limit;
		}

//		echo $sql;
		$query = $this->driver->query($sql);
		return $query->rows;
  	}	
	
	public function add($table_name, $post_data = array(), $post_names = array()) {
		$all_columns = $this->one($table_name);
		$all_columns = array_keys($all_columns);
		
		print_r($all_columns);
		
		$data = "SET ";
		
		if(is_array($post_names) && count($post_names) > 0) {
			foreach($post_names as $each_name => $each_column) {
				
				if(empty($each_name))
					$each_name = $each_column;
				
				if(isset($post_data[$each_name]))	
					$data .= " `$each_column` = '".$this->escape($post_data[$each_name])."', ";
			}
		} else if(is_array($post_data) && count($post_data) > 0) {
			foreach($all_columns as $each_column) {
				
				if(isset($post_data[$each_column]))
					$data .= " `$each_column` = '".$this->escape($post_data[$each_column])."', ";
			}
		} else {
			foreach($all_columns as $each_column) {
				if(!empty($_POST[$each_column]))
					$data .= " `$each_column` = '".$this->escape($_POST[$each_column])."', ";
			}		
		}
		
		$data = substr($data,0,strlen($data)-2).' ';
		
		$sql = "INSERT INTO $table_name $data";

		return $this->driver->query($sql);	
	}
	
	public function update($table_name, $conditions = array(), $post_data = array(), $post_names = array()) {

		$data = "SET ";
		$condition = " WHERE 1=1";
		$all_columns = $this->one($table_name);
		$all_columns = array_keys($all_columns);

		if(is_array($conditions) && count($conditions) > 0) :
		foreach($conditions as $name => $value) {
			$name = $this->escape($name); 
			$value = $this->escape($value);
			$condition .= " AND `$name` = '$value' ";
		}
		endif;
		
		if(is_array($post_names) && count($post_names) > 0) {
			foreach($post_names as $each_column => $each_name) {
				$data .= " `$each_column` = '".$this->escape($post_data[$each_name])."', ";
			}
		} else if(is_array($post_data) && count($post_data) > 0) {
			foreach($post_data as $each_column => $each_value) {
				$data .= " `$each_column` = '".$this->escape($each_value)."', ";
			}
		} else {
			foreach($all_columns as $each_column) {
				if(!empty($post_data[$each_column])) {
					
					$data .= " `$each_column` = '".$this->escape($post_data[$each_column])."', ";
				}
			}		
		}
		
		$data = substr($data,0,strlen($data)-2).' ';
		
		$sql = "UPDATE $table_name $data $condition";
//		echo $sql;
		return $this->driver->query($sql);
	}
	
	public function delete($table_name, $conditions = array(), $columns = array()) {
		$column = "";
		$condition = "";
		
		if(is_array($conditions) && count($conditions) > 0) :
		$condition = " WHERE 1=1 ";
		foreach($conditions as $name => $value) {
			$condition .= " AND `$name` = '$value' ";
		}		
		endif;
		
		if(is_array($columns) && count($columns) > 0) {
			
			foreach($columns as $name) {
				$column .= ' `$name`, ';
			}
				$column = substr($column,0,strlen($column)-2).' ';

				$sql = "DELETE $column FROM `$table_name`".$condition." LIMIT 1";
		} else if(!empty($columns)) {
				$sql = "DELETE $columns FROM `$table_name`".$condition." LIMIT 1";
		} else {
				$sql = "DELETE  FROM `$table_name`".$condition." LIMIT 1";
		}
		
		return $this->driver->query($sql);
  	}
	
	public function deleteAll($table_name, $conditions = array(), $limits = array(), $columns = array()) {
		$limit = "";
		$column = "";
		$condition = "";

		if(is_array($conditions) && count($conditions) > 0) :
		$condition = " WHERE 1=1 ";
		foreach($conditions as $name => $value) {
			$condition .= " AND `$name` = '$value' ";
		}
		endif;
		
		if(is_array($limits) && count($limits) > 0) {
			$page = (int) $limits[0] - 1;
			$views = (int) $limits[1] - 1;		
			$limit = " LIMIT $page , $views";
		} else if(!empty($limits)) {
				$limit = " LIMIT $limits ";
		}
		
		if(is_array($columns) && count($columns) > 0) {
			
			foreach($columns as $name) {
				$column .= ' `$name`, ';
			}
				$column = substr($column,0,strlen($column)-2).' ';

				$sql = "DELETE $column FROM `$table_name`".$condition.$limit;
		} else if(!empty($columns)) {
				$sql = "DELETE $columns FROM `$table_name`".$condition.$limit;
		} else {
				$sql = "DELETE  FROM `$table_name`".$condition.$limit;
		}

//		echo $sql;
		return $this->driver->query($sql);
  	}

	public function zip($data) {
		return serialize($data);	
	}
	
	public function unzip($data) {
		return unserialize($data);
	}
}

$db = new DB(DB_DRIVER, DB_HOST, DB_USER, DB_PASS, DB_NAME);
?>
